package com.jarvis.app.car.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Objects;

public class DBUtils {
    //自定义连接池
    private static String url = "jdbc:mysql://rm-bp1s2g4new27oqxosjo.mysql.rds.aliyuncs.com:3306/jarvis_shop?rewriteBatchedStatements=true&serverTimezone=UTC&logger=com.mysql.jdbc.log.StandardLogger";
    private static String username = "yichen";
    private static String password = "@Yichen20220302";
    private static String driverClassName = "com.mysql.jdbc.Driver";

    static {
        try {
            Class.forName(driverClassName).newInstance();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        }
    }


    public static Connection getConnection() throws SQLException {

        return DriverManager.getConnection(url, username, password);

    }

    public static void close(AutoCloseable... autoCloseables) throws Exception {
        for (AutoCloseable autoCloseable : autoCloseables) {
            if (Objects.nonNull(autoCloseable)) {
                autoCloseable.close();
            }
        }
    }


}
